﻿package com.tudou.v2
{
    import com.tudou.config.*;
    import com.tudou.consts.*;
    import com.tudou.events.*;
    import com.tudou.model.*;
    import com.tudou.track.*;
    import com.tudou.utils.*;
    import flash.events.*;
    import flash.net.*;

    public class V2Loader extends EventDispatcher
    {
        protected var videoInfo:VideoInfoCompatV2;
        protected var loadCount:int = 0;
        protected var loader:URLLoader;
        private var LOAD_COUNT_MAX:int = 2;
        protected var baseInfo:IBaseInfo;
        protected var password:String = "";
        protected var _url:String;

        public function V2Loader()
        {
            return;
        }// end function

        protected function v2SecurityErrHlr(event:SecurityErrorEvent) : void
        {
            ErrorReportTracker.send(baseInfo.iid, ErrorReportTracker.V2_SECURITY_ERROR, "securityErr");
            ErrorTracker.send(baseInfo.sourceId, ErrorTracker.V2_SECURITY_ERROR, 0, url);
            dispatchEvent(new V2LoaderEvent(V2LoaderEvent.SECURITY_ERROR, false, false, event.text));
            if (loadCount < LOAD_COUNT_MAX)
            {
                startLoad();
            }
            else
            {
                ErrorReportTracker.send(baseInfo.iid, ErrorReportTracker.CAN_NOT_PLAY, "securityErr", ErrorReportTracker.CAN_NOT_PLAY_V2_CROSSDOMAIN_ERROR);
                ErrorTracker.send(baseInfo.sourceId, ErrorTracker.CAN_NOT_PLAY, ErrorTracker.CAN_NOT_PLAY_V2_CROSSDOMAIN_ERROR, url);
            }
            return;
        }// end function

        protected function v2IOErrHlr(event:IOErrorEvent) : void
        {
            ErrorReportTracker.send(baseInfo.iid, ErrorReportTracker.V2_IOERROR, "ioerror");
            ErrorTracker.send(baseInfo.sourceId, ErrorTracker.V2_IOERROR, 0, url);
            dispatchEvent(new V2LoaderEvent(V2LoaderEvent.IO_ERROR, false, false, event.text));
            if (loadCount < LOAD_COUNT_MAX)
            {
                startLoad();
            }
            else
            {
                ErrorReportTracker.send(baseInfo.iid, ErrorReportTracker.CAN_NOT_PLAY, "ioerror", ErrorReportTracker.CAN_NOT_PLAY_V2_ERROR);
                ErrorTracker.send(baseInfo.sourceId, ErrorTracker.CAN_NOT_PLAY, ErrorTracker.CAN_NOT_PLAY_V2_ERROR, url);
            }
            return;
        }// end function

        protected function get currType() : Array
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_1:Array = [VideoInfo.NORMAL_256_FLV];
            var _loc_2:Array = [];
            _loc_2[RateType.P256] = VideoInfo.NORMAL_256_F4V;
            _loc_2[RateType.P360] = VideoInfo.HD_360;
            _loc_2[RateType.P480] = VideoInfo.HD_480;
            _loc_2[RateType.P720] = VideoInfo.HD_720;
            _loc_2[RateType.ORIGINAL] = VideoInfo.ORIGINAL;
            var _loc_3:* = RateUtil.getAvailable(baseInfo.maxRateType, baseInfo.original);
            while (_loc_5 < _loc_3.length)
            {
                
                _loc_4 = _loc_3[_loc_5];
                _loc_1.push(_loc_2[_loc_4]);
                _loc_5++;
            }
            return _loc_1;
        }// end function

        protected function v2LoadedHlr(event:Event) : void
        {
            videoInfo = new VideoInfoCompatV2(loader.data, baseInfo.sourceId, baseInfo.iid, baseInfo.maxRateType, baseInfo.original, url);
            dispatchEvent(new V2LoaderEvent(V2LoaderEvent.COMPLETE, false, false, videoInfo));
            return;
        }// end function

        public function getVideoInfo() : VideoInfo
        {
            return videoInfo;
        }// end function

        public function set tryTimes(param1:int) : void
        {
            LOAD_COUNT_MAX = param1;
            return;
        }// end function

        public function get xml()
        {
            return loader.data;
        }// end function

        protected function startLoad() : void
        {
            close();
            loader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, v2LoadedHlr);
            loader.addEventListener(IOErrorEvent.IO_ERROR, v2IOErrHlr);
            loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, v2SecurityErrHlr);
            var _loc_1:* = new URLRequest(Config.V2_URL);
            var _loc_2:* = new URLVariables();
            _loc_2.si = baseInfo.sourceId;
            _loc_2.vn = "02";
            _loc_2.it = baseInfo.iid;
            _loc_2.pw = password;
            _loc_2.ui = baseInfo.userId;
            _loc_2.st = currType.join(",");
            _loc_2.refurl = baseInfo.href;
            _loc_2.hd = baseInfo.currRateType + 1;
            if (baseInfo.v2pr)
            {
                _loc_2.pr = baseInfo.v2pr;
            }
            _loc_2.noCache = RandString.noCache;
            _loc_1.method = URLRequestMethod.GET;
            _loc_1.data = _loc_2;
            loader.load(_loc_1);
            _url = _loc_1.url + "?" + _loc_2.toString();
            var _loc_4:* = loadCount + 1;
            loadCount = _loc_4;
            return;
        }// end function

        public function load(param1:IBaseInfo, param2:String) : void
        {
            this.baseInfo = param1;
            this.password = param2;
            loadCount = 0;
            videoInfo = null;
            startLoad();
            return;
        }// end function

        public function get url() : String
        {
            return _url;
        }// end function

        public function close() : void
        {
            if (loader != null)
            {
                loader.close();
                loader.removeEventListener(Event.COMPLETE, v2LoadedHlr);
                loader.removeEventListener(IOErrorEvent.IO_ERROR, v2IOErrHlr);
                loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, v2SecurityErrHlr);
                loader = null;
            }
            return;
        }// end function

    }
}
